/**
 * 
 */
package com.wolfpire.lessonplan.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.wolfpire.knowledge.dao.SubjectKnowledgeRelationDao;
import com.wolfpire.lessonplan.dao.SubjectLessonPlanRelationDao;
import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.system.common.base.dao.impl.BaseHibernateDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.SubjectKnowledgeRelation;
import com.wolfpire.system.model.SubjectLessonPlanRelation;

/**
 * @author lihd
 *
 */

@Repository("subjectLessonPlanRelationDao")
public class SubjectLessonPlanRelationDaoImpl extends BaseHibernateDao<SubjectLessonPlanRelation, Long> implements SubjectLessonPlanRelationDao {

	public void relate(Long subjectId, Long LessonplanId, Long knowledgeId) {
		//先删除后绑定
		this.deleteByLessonPlanId(LessonplanId);
		if (null != subjectId) {
			SubjectLessonPlanRelation sk = new SubjectLessonPlanRelation(subjectId,LessonplanId,knowledgeId);
			this.saveOrUpdate(sk);
		}
	}
	
	private void deleteByLessonPlanId(Long LessonplanId) {
		String executeSql = "DELETE FROM t_subject_lessonplan WHERE lessonplan_id = ?";
		this.executeSql(executeSql, LessonplanId);
			
	}

	public SubjectLessonPlanRelation getSPRelation(Long lessonplanId) {
		Criteria c = createCriteria(Restrictions.eq("lessonplanId", lessonplanId));
		Object obj = c.uniqueResult();
		if (null != obj) {
			return (SubjectLessonPlanRelation) obj;
		}
		return null;
	}

	public List<SubjectLessonPlanRelation> getPSRelations(Long subjectId) {
		Criteria c = createCriteria(Restrictions.eq("subjectId", subjectId), Restrictions.isNotNull("LessonplanId"));
		return c.list();
	}

}
